const { ipcRenderer } = require("electron");
const $ = require("jquery");

const file = read_data(path.resolve(), "file");

const json_file = path.join(file.file_data, "data");

let choose = false;

document.addEventListener("DOMContentLoaded", () => {
    //禁止a标签被拖拽
    $("body").on("mousedown", "a", function (e) {
        e.preventDefault();
    });

    //处理页面切换
    $(".nav-item").on("click", function () {
        const target = $(this).data("target");
        $(".body_page").hide();
        $("#" + target).show();
    });

    //接收选择的文件夹并进行处理
    ipcRenderer.on("dialog_path", (ev, data) => {
        if (!data) {
            choose = false;
            return;
        }
        const fs = require("fs");
        const all_img_name = fs.readdirSync(data[0]);
        for (let i in all_img_name) {
            if (all_img_name[i] == "app.js") {
                $("." + choose).attr("value", data[0]);
                choose = false;
                return;
            }
        }
        $("." + choose).addClass("warn");
        $("." + choose).attr("placeholder", "请选择正确的文件夹！");
        choose = false;
    });
});

//关闭页面
function closeapp() {
    ipcRenderer.send("close_add_bot");
}

//显示选择文件的弹窗
function dialog_path(data) {
    choose = data;
    ipcRenderer.send("addbot_dialog_path", "选择云崽");
}

//创建处理
function Press_button(data) {
    const initial_form = $("#" + data).serialize();
    const handle1 = initial_form.split("&");
    let form = {},
        complete = true;
    for (let handle2 of handle1) {
        const i = handle2.split("=");
        form[i[0]] = i[1];
        if (i[1] == "") {
            complete = false;
            $("#" + data + " ." + i[0]).addClass("warn");
            $("#" + data + " ." + i[0]).attr("placeholder", "请不要留空！");
        }
    }
    if (!complete) return;
    const Profiles = read_data(json_file, "Profiles");
    const list = read_data(json_file, "list");
    const Information = {
        id: Profiles.num,
        title: form.title,
        notes: form.notes,
        redis_port: form.redis,
        bot_port: false,
        bot_path: false
    };
    if (data == "set_bot") {
        Information.bot_port = Profiles.bot_port;
        Information.redis_port = Profiles.redis_port;
        let Developers = ``,
            prefix = ``;
        if (form.bot == "Trss") {
            Developers = "TimeRainStarSky";
        }
        if (form.bot == "Miao") {
            Developers = "yoimiya-kokomi";
            prefix = "Miao-";
        }
        ipcRenderer.send("execute_add_bot", {
            url: `https://${form.install}.com/${Developers}/${prefix}Yunzai`,
            id: Information.id
        });
    }
    let new_bot = true
    if (data == "import_bot") {
        Information.bot_path = decodeURIComponent(form.path);
        const number = Number(Information.redis_port)
        if ((Information.redis_port > 65535 || Information.redis_port < 1023) || number != Information.redis_port) {
            console.log(Information.redis_port > 65535);
            console.log(Information.redis_port < 1023);
            console.log(typeof Information.redis_port !== 'number');

            $("#" + data + " .redis").val("");
            $("#" + data + " .redis").addClass("warn");
            $("#" + data + " .redis").attr("placeholder", "请输入正确的端口号");
            return
        }
        new_bot = false
    }
    Profiles.num++;
    Profiles.bot_port++;
    Profiles.redis_port++;
    list.push(Information);
    write_data(json_file, "list", list);
    write_data(json_file, "Profiles", Profiles);
    ipcRenderer.send("close_add_bot", new_bot);
}